//
//  IMAlertView.m
//  BaseProject
//
//  Created by 王洋 on 2021/2/1.
//  Copyright © 2021 王洋. All rights reserved.
//
#import "TTTAttributedLabel.h"

#import "IMAlertView.h"

@interface IMAlertView ()

@property (weak, nonatomic) IBOutlet UIImageView *mainIcon;

@property (weak, nonatomic) IBOutlet UIButton *handleBtn;

@property (weak, nonatomic) IBOutlet UIView *contentBG;

@property (weak, nonatomic) IBOutlet UIView *bgView;

@property (weak, nonatomic) IBOutlet UILabel *callLab;

@property (weak, nonatomic) IBOutlet UILabel *contentLab;

@end

@implementation IMAlertView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    // gradient
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0,0,TTScreenWidth,305);
    gl.startPoint = CGPointMake(0.5, 0);
    gl.endPoint = CGPointMake(0.5, 1);
    gl.colors = @[(__bridge id)[UIColor colorWithRed:255/255.0 green:242/255.0 blue:241/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor];
    gl.locations = @[@(0), @(0.2f), @(1.0f)];
    
    [self.bgView.layer insertSublayer:gl atIndex:0];
    
    
    NSString *str = @"看您浏览挺久的，累了吧，是否需要限时免费的婚礼统筹师解答服务，给到您对应的解答和建议，帮助您快速解决难题。";
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:str];
    [string addAttribute:NSForegroundColorAttributeName value:UIColorHex(0xff4242) range:NSMakeRange(16,4)];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:10];//设置距离
    [string addAttribute:NSParagraphStyleAttributeName
                            value:paragraphStyle
                            range:NSMakeRange(0, str.length)];
    
    self.contentLab.attributedText = string;
    
    WEAKSELF
    self.callLab.userInteractionEnabled = YES;
    [self.callLab addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
        if (weakSelf.IMAlertViewBlock) {
            weakSelf.IMAlertViewBlock(IMAlertViewActionType_call);
        }
    }];
    
    self.userInteractionEnabled = YES;
    [self addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
        if (weakSelf.IMAlertViewBlock) {
            weakSelf.IMAlertViewBlock(IMAlertViewActionType_close);
        }
    }];
    
    [self.bgView addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
        if (weakSelf.IMAlertViewBlock) {
            weakSelf.IMAlertViewBlock(IMAlertViewActionType_handle);
        }
    }];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.mainIcon pp_clipCorners:UIRectCornerAllCorners radius:self.mainIcon.frame.size.height/2 border:3 color:UIColorHex(ffffff)];
        [self.handleBtn pp_clipCorners:UIRectCornerAllCorners radius:self.handleBtn.frame.size.height/2];
        [self.bgView pp_clipCorners:UIRectCornerAllCorners radius:10];
        [self.contentBG pp_clipCorners:UIRectCornerAllCorners radius:6];
    });
    
}

- (IBAction)callAction:(id)sender {
    if (self.IMAlertViewBlock) {
        self.IMAlertViewBlock(IMAlertViewActionType_call);
    }
}

- (IBAction)handleAction:(id)sender {
    if (self.IMAlertViewBlock) {
        self.IMAlertViewBlock(IMAlertViewActionType_handle);
    }
}

- (IBAction)closeAction:(id)sender {
    if (self.IMAlertViewBlock) {
        self.IMAlertViewBlock(IMAlertViewActionType_close);
    }
}

@end
